package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.PaOrder;
import com.ruoyi.system.domain.PaOrderItem;
import com.ruoyi.system.domain.PaUserAddrOrder;
import com.ruoyi.system.domain.bo.PaOrderItemBo;
import com.ruoyi.system.domain.bo.PxBoxBo;
import com.ruoyi.system.domain.bo.SystemOrderRequestBo;
import com.ruoyi.system.domain.bo.UserOrderBo;
import com.ruoyi.system.domain.vo.UserOrderVo;
import com.ruoyi.system.mapper.PaOrderItemMapper;
import com.ruoyi.system.mapper.PaOrderMapper;
import com.ruoyi.system.mapper.PaUserAddrOrderMapper;
import com.ruoyi.system.service.IPxBoxService;
import com.ruoyi.system.service.IUserOrderService;
import com.ruoyi.system.service.IUserPxBoxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName IUserOrderServiceImpl
 * @Description app订单服务
 * @date 2024/7/3 19:55
 * @Version 1.0
 * @Auther: zhangzhenyu
 */
@Service
public class UserOrderServiceImpl implements IUserOrderService {

    @Autowired
    private PaOrderMapper paOrderMapper;

    @Autowired
    private PaOrderItemMapper paOrderItemMapper;

    @Autowired
    private PaUserAddrOrderMapper paUserAddrOrderMapper;

    @Autowired
    private IUserPxBoxService userPxBoxService;

    @Autowired
    private IPxBoxService pxBoxService;


    @Override
    public Long insertPaOrder(SystemOrderRequestBo userOrderBo) {
        //获取用户信息
        // 使用雪花算法生成的订单号
//        String orderNumber = String.valueOf(new Snowflake(1, 1).nextId());
//        userOrderBo.setOrderNumber(orderNumber);
        PaOrder paOrder = new PaOrder();
        BeanUtils.copyBeanProp(paOrder, userOrderBo);
        paOrder.setStatus(1);
        List<PaOrderItemBo> paOrderItemBos = userOrderBo.getPaOrderItems();
        String prodName = paOrderItemBos.stream()
                .map(PaOrderItemBo::getProdName)
                .collect(Collectors.joining(", "));
        paOrder.setProdName(prodName);
        paOrder.setCreateBy(String.valueOf(userOrderBo.getUserId()));
        //获取paOrderItemBos里面的 产品个数和产品单价 并相乘
      /*  int prodCount = paOrderItemBos.stream()
                .map(PaOrderItemBo::getProdCount)
                .mapToInt(Long::intValue) // 显式转换Long到int
                .sum();

        double unintPrice = paOrderItemBos.stream()
                .filter(Objects::nonNull) // 过滤掉null值
                .map(PaOrderItemBo::getUnintPrice)
                .mapToDouble(BigDecimal::doubleValue)
                .sum();
        // prodCount 和 actualTotal 相乘
        double actualTotal = prodCount * unintPrice;
        paOrder.setActualTotal(new BigDecimal(String.valueOf(actualTotal)));*/
        paOrderMapper.insertPaOrder(paOrder);
        Long orderId = paOrder.getOrderId();
        paOrderItemBos.forEach(x -> {
            x.setOrderId(Long.valueOf(orderId));
//            x.setOrderNumber(orderNumber);
            PaOrderItem paOrderItem = new PaOrderItem();
            BeanUtils.copyBeanProp(paOrderItem, x);
            paOrderItemMapper.insertPaOrderItem(paOrderItem);
        });
        // 获取paUserAddrs对象列表 并保存到数据库
        List<PaUserAddrOrder> paUserAddrs = userOrderBo.getPaUserAddrs();
        paUserAddrs.forEach(paUserAddrOrder -> {
            paUserAddrOrder.setOrderId(Long.valueOf(orderId));
            paUserAddrOrder.setCreateTime(DateUtils.getNowDate());
            paUserAddrOrder.setUpdateTime(DateUtils.getNowDate());
            paUserAddrOrderMapper.insertPaUserAddrOrder(paUserAddrOrder);
        });
        //保存拼箱
        if (userOrderBo.getPxBox() != null) {
            userOrderBo.getPxBox().setOrderId(orderId);
            userOrderBo.getPxBox().setCreateBy(String.valueOf(userOrderBo.getUserId()));
            userOrderBo.getPxBox().setUserId(paOrder.getSupplierId());
            userPxBoxService.insertPxBox(userOrderBo.getPxBox());
        }
        return orderId;
    }

    @Override
    public TableDataInfo<UserOrderVo> selectPaOrderList(UserOrderBo userOrderBo) {
        List<UserOrderVo> userOrderVos = new ArrayList<>();
        PaOrder paOrder = new PaOrder();
        BeanUtils.copyBeanProp(paOrder, userOrderBo);
        PageUtils.startPage();
        List<PaOrder> paOrders = paOrderMapper.selectPaOrderList(paOrder);
        PageInfo<PaOrder> pageInfo = PageInfo.of(paOrders);
        for (PaOrder a : paOrders) {
            UserOrderVo userOrderVo = new UserOrderVo();
            BeanUtils.copyBeanProp(userOrderVo, a);

            //获取产品信息
            PaOrderItem paOrderItem = new PaOrderItem();
            paOrderItem.setOrderId(a.getOrderId());
            List<PaOrderItem> paOrderItems = paOrderItemMapper.selectPaOrderItemList(paOrderItem);
            List<PaOrderItemBo> paOrderItemBos = BeanUtil.copyToList(paOrderItems, PaOrderItemBo.class);
            //将报价置空
            paOrderItemBos.forEach(x -> {
                x.setUnintPrice(null);
            });
            userOrderVo.setPaOrderItems(paOrderItemBos);

            //获取地址信息
            PaUserAddrOrder paUserAddrOrder = new PaUserAddrOrder();
            paUserAddrOrder.setOrderId(a.getOrderId());
            List<PaUserAddrOrder> paUserAddrOrders = paUserAddrOrderMapper.selectPaUserAddrOrderList(paUserAddrOrder);
            userOrderVo.setPaUserAddrs(paUserAddrOrders);
            userOrderVo.setPxBox(pxBoxService.selectPxBoxByOrderId(a.getOrderId()));
            userOrderVos.add(userOrderVo);
        }
        TableDataInfo<UserOrderVo> page = TableDataInfo.build(userOrderVos);
        page.setTotal(pageInfo.getTotal());
        return page;
    }

    @Override
    public UserOrderVo selectPaOrderByOrderId(Long orderId, Long userId) {
        UserOrderVo userOrderVo = new UserOrderVo();
        PaOrder paOrder = paOrderMapper.selectPaOrderByOrderId(orderId);
        if (Objects.isNull(paOrder)) {
            return null;
        }
        // 判断当用登录用户和 paOrder对象的userId 是否一致
        if (userId.equals(paOrder.getUserId())) {
            //就是用户
            BeanUtils.copyBeanProp(userOrderVo, paOrder);
            userOrderVo.setTotal(null);
            userOrderVo.setUseType(0);
            //根据paOrder对象的 orderId 获取产品信息
            PaOrderItem paOrderItem = new PaOrderItem();
            paOrderItem.setOrderId(orderId);
            List<PaOrderItem> paOrderItems = paOrderItemMapper.selectPaOrderItemList(paOrderItem);
            //循环要把paOrderItems 单价设置为空
            paOrderItems.forEach(x -> {
                x.setUnintPrice(null);
            });
            List<PaOrderItemBo> paOrderItemBos = BeanUtil.copyToList(paOrderItems, PaOrderItemBo.class);
            userOrderVo.setPaOrderItems(paOrderItemBos);
            userOrderVo.setUseType(0);
            // 查询地址信息并且赋值
            PaUserAddrOrder paUserAddrOrder = new PaUserAddrOrder();
            paUserAddrOrder.setOrderId(orderId);
            List<PaUserAddrOrder> paUserAddrOrders = paUserAddrOrderMapper.selectPaUserAddrOrderList(paUserAddrOrder);
            userOrderVo.setPaUserAddrs(paUserAddrOrders);
            //根据orderId 查询拼箱信息
            userOrderVo.setPxBox(pxBoxService.selectPxBoxByOrderId(orderId));
            return userOrderVo;
        } else {
            //供应商
            BeanUtils.copyBeanProp(userOrderVo, paOrder);
            userOrderVo.setActualTotal(null);
            userOrderVo.setUseType(1);
            //根据paOrder对象的 orderId 获取产品信息
            PaOrderItem paOrderItem = new PaOrderItem();
            paOrderItem.setOrderId(orderId);
            List<PaOrderItem> paOrderItems = paOrderItemMapper.selectPaOrderItemList(paOrderItem);
            List<PaOrderItemBo> paOrderItemBos = BeanUtil.copyToList(paOrderItems, PaOrderItemBo.class);
            //循环要把paOrderItems 单价设置为空
//            paOrderItemBos.forEach(x -> {
//                x.setActualUnintPrice(null);
//            });
            userOrderVo.setPaOrderItems(paOrderItemBos);
            //根据orderId 查询拼箱信息
            userOrderVo.setPxBox(pxBoxService.selectPxBoxByOrderId(orderId));
            return userOrderVo;
        }

    }

    @Override
    public int updatePaOrder(SystemOrderRequestBo userOrderBo) {

        // 更新paOrder
        PaOrder param = new PaOrder();
        BeanUtils.copyBeanProp(param, userOrderBo);
        List<PaOrderItemBo> paOrderItemBos = userOrderBo.getPaOrderItems();
        String prodName = paOrderItemBos.stream()
                .map(PaOrderItemBo::getProdName)
                .collect(Collectors.joining(", "));
        param.setProdName(prodName);
        paOrderMapper.updatePaOrder(param);
        //更新产品列表
        List<PaOrderItemBo> paOrderItemBo = userOrderBo.getPaOrderItems();
        paOrderItemBo.forEach(x -> {
            x.setOrderId(userOrderBo.getOrderId());
            if (Objects.isNull(x.getOrderItemId())) {
                paOrderItemMapper.insertPaOrderItem(x);
            } else {
                paOrderItemMapper.updatePaOrderItem(x);
            }
        });
        // 更新地址信息
        List<PaUserAddrOrder> paUserAddrs = userOrderBo.getPaUserAddrs();
        if(CollectionUtil.isNotEmpty(paUserAddrs)) {
            paUserAddrs.forEach(paUserAddrOrder -> {
                paUserAddrOrder.setOrderId(userOrderBo.getOrderId());
                paUserAddrOrderMapper.updatePaUserAddrOrder(paUserAddrOrder);
            });
        }
        //修改拼箱信息
        PxBoxBo pxBoxBo = userOrderBo.getPxBox();
        if (Objects.nonNull(pxBoxBo)) {
            userPxBoxService.updatePxBox(pxBoxBo);
        }

        return 1;
    }

    @Override
    public PaOrder selectByOrderId(Long orderId) {
        return paOrderMapper.selectPaOrderByOrderId(orderId);
    }
}
